﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace csharp_mysql
{
    class Program
    {
        static void Main(string[] args)
        {
            string strConn = "Server=localhost;Database=test;Uid=root;Pwd=";
            MySqlConnection conn = new MySqlConnection(strConn);

            SelectUsingReader(conn);

            conn.Close();

            SelectUsingAdapter(conn);

            Console.ReadKey();
        }

        static void SelectUsingReader(MySqlConnection conn)
        {
            conn.Open();
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM sample", conn);
            MySqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                Console.WriteLine("{0} {1} {2}", rdr["ID"], rdr["NAME"], rdr["DESCRIPTION"]);
            }
            rdr.Close();
        }

        static void SelectUsingAdapter(MySqlConnection conn)
        {
            DataSet ds = new DataSet();
            MySqlDataAdapter adpt = new MySqlDataAdapter("SELECT * FROM sample", conn);
            adpt.Fill(ds);

            foreach (DataRow r in ds.Tables[0].Rows)
            {
                Console.WriteLine("{0} {1} {2}", r["ID"], r["NAME"], r["DESCRIPTION"]);
            }
        }
    }
}
